package javagprssrv.Serial;

import javax.comm.SerialPortEvent;
import javagprssrv.StructClass.structCOMMTerm;
import javagprssrv.DataAnalyse.ScriptExecuter;
import javagprssrv.Log;
import javagprssrv.JGS_Param;
import javax.comm.NoSuchPortException;
import javax.comm.UnsupportedCommOperationException;
import java.io.IOException;
import javax.comm.SerialPortEventListener;
import java.io.InputStream;
import java.io.OutputStream;
import javax.comm.SerialPort;
import javax.comm.CommPortIdentifier;

public class SerialBean
{
    public int state;
    public int PortNumber;
    javax.comm.CommPortIdentifier portId;
    javax.comm.SerialPort serialPort;
    java.lang.String PortName;
    java.io.OutputStream out;
    java.io.InputStream in;
    
    public SerialBean(final int PortID) {
        this.state = 0;
        this.PortNumber = PortID;
        this.PortName = "COM" + PortID;
    }
    
    public int Initialize() {
        final int InitSuccess = 1;
        final int InitFail = -1;
        try {
            this.portId = javax.comm.CommPortIdentifier.getPortIdentifier(this.PortName);
            try {
                (this.serialPort = (javax.comm.SerialPort)this.portId.open("Serial_Communication", 2000)).notifyOnDataAvailable(true);
                this.serialPort.enableReceiveTimeout(1000);
                this.serialPort.addEventListener((javax.comm.SerialPortEventListener)new javagprssrv.Serial.SerialBean.MyPortListener());
            }
            catch (java.lang.Exception e) {
                return InitFail;
            }
            try {
                this.in = this.serialPort.getInputStream();
                this.out = this.serialPort.getOutputStream();
            }
            catch (java.io.IOException e2) {
                return InitFail;
            }
            try {
                this.serialPort.setSerialPortParams(9600, 8, 1, 0);
            }
            catch (javax.comm.UnsupportedCommOperationException e3) {
                return InitFail;
            }
        }
        catch (javax.comm.NoSuchPortException e4) {
            return InitFail;
        }
        return InitSuccess;
    }
    
    private void ProcessDataArrival(final int state, final java.lang.String PortName, final java.lang.String strRecv, final javagprssrv.Serial.SerialBean SB) {
        javagprssrv.JGS_Param.myLog.ShowLog("[P=" + PortName + ",T=GET " + strRecv.length() + "] " + strRecv, true, true);
        if (state == 0) {
            boolean isUseAble = false;
            isUseAble = this.isUseAbleCOMM(strRecv);
            if (isUseAble) {
                this.SaveUseAbleCOMM(SB, strRecv);
                new javagprssrv.Log().ShowLog(SB.PortName + "：系统已成功将其注册为可用串口设备。", true, true);
                SB.WritePort("atv0\r\n");
            }
        }
    }
    
    private boolean isUseAbleCOMM(final java.lang.String strRecv) {
        boolean isUsable = false;
        try {
            final javagprssrv.DataAnalyse.ScriptExecuter se = new javagprssrv.DataAnalyse.ScriptExecuter("COMMAction.js");
            final int result = java.lang.Float.valueOf(java.lang.String.valueOf(se.Execute(0, "RecvData=" + strRecv))).intValue();
            if (result > -1) {
                isUsable = true;
            }
        }
        catch (java.lang.Exception e) {
            e.printStackTrace();
        }
        return isUsable;
    }
    
    private void SaveUseAbleCOMM(final javagprssrv.Serial.SerialBean SB, final java.lang.String strRecv) {
        final javagprssrv.StructClass.structCOMMTerm COMMTerm = new javagprssrv.StructClass.structCOMMTerm();
        COMMTerm.PortNumber = SB.PortNumber;
        COMMTerm.PortName = SB.PortName;
        COMMTerm.TermInfo = strRecv;
        COMMTerm.SB = SB;
        COMMTerm.isFree = true;
        COMMTerm.timesDial = 0;
        COMMTerm.timesSendMessage = 0;
        javagprssrv.JGS_Param.COMMTerms.add(COMMTerm);
    }
    
    public java.lang.String ReadPort(final int Length) {
        final java.lang.String Msg = "";
        return Msg;
    }
    
    public void WritePort(final java.lang.String Msg) {
        try {
            for (int i = 0; i < Msg.length(); ++i) {
                this.out.write(Msg.charAt(i));
            }
            javagprssrv.JGS_Param.myLog.ShowLog("[P=" + this.PortName + ",T=SEND " + Msg.length() + "] " + Msg, true, true);
        }
        catch (java.io.IOException ex) {}
    }
    
    public void ClosePort() {
        this.serialPort.close();
    }
    
    public void Dial(final java.lang.String TelNO, final int DialSeconds) {
        java.lang.String S = "ATD" + TelNO + ";\r\n";
        this.WritePort(S);
        try {
            java.lang.Thread.sleep(DialSeconds * 1000);
        }
        catch (java.lang.Exception ex) {}
        S = "ATH\r\n";
        this.WritePort(S);
    }
    
    public void SendSMS(final java.lang.String TelNO, final java.lang.String SMSTxt) {
    }
    
    public void COMM_SetReturnAsString() {
        this.WritePort("atv1\r\n");
    }
    
    public void COMM_SetReturnAsInt() {
        this.WritePort("atv0\r\n");
    }
    
    public void COMM_Dial(final java.lang.String TelNo, final int DialDelaySeconds) {
        this.WritePort("atd" + TelNo + ";\r\n");
        try {
            java.lang.Thread.sleep(DialDelaySeconds * 1000);
        }
        catch (java.lang.Exception ex) {}
        this.WritePort("ath\r\n");
        try {
            java.lang.Thread.sleep(300L);
        }
        catch (java.lang.Exception ex2) {}
    }
    
    public void COMM_SendMessage(final java.lang.String TelNo, final java.lang.String SMSText) {
    }
    
    public void COMM_GPRS_OnLine(final java.lang.String GPRSOnLineDialTelNo) {
    }
    
    public void COMM_GPRS_Write(final java.lang.String RemoteIP, final int RemotePort, final java.lang.String strData) {
    }
    
    static /* synthetic */ void access000(final javagprssrv.Serial.SerialBean x0, final int x1, final java.lang.String x2, final java.lang.String x3, final javagprssrv.Serial.SerialBean x4) {
        x0.ProcessDataArrival(x1, x2, x3, x4);
    }
    
    class MyPortListener implements javax.comm.SerialPortEventListener
    {
        public void serialEvent(final javax.comm.SerialPortEvent evt) {
            switch (evt.getEventType()) {
                case 3: {}
                case 6: {}
                case 10: {}
                case 4: {}
                case 9: {}
                case 7: {}
                case 8: {}
                case 5: {}
                case 1: {
                    java.lang.String strRecv = "";
                    try {
                        int ch;
                        while ((ch = javagprssrv.Serial.SerialBean.this.in.read()) > 0) {
                            final java.lang.Character d = new java.lang.Character((char)ch);
                            strRecv = strRecv.concat(d.toString());
                        }
                        javagprssrv.Serial.SerialBean.this.ProcessDataArrival(javagprssrv.Serial.SerialBean.this.state, javagprssrv.Serial.SerialBean.this.PortName, strRecv, javagprssrv.Serial.SerialBean.this);
                        if (javagprssrv.Serial.SerialBean.this.state == 0) {
                            javagprssrv.Serial.SerialBean.this.state = 1;
                        }
                    }
                    catch (java.io.IOException ex) {}
                    break;
                }
            }
        }
    }
}
